using MallSystembackend.Api.Repository;
using MallSystembackend.Api.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MallSystembackend.Api.ParamModel;
using System.Linq;
namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CommodityController : ControllerBase
    {
        public IRepository<Commodity> _commodityRepository;
        public readonly IConfiguration _configuration;

        public CommodityController(IRepository<Commodity> commodityRepository, IConfiguration configuration)
        {
            _commodityRepository = commodityRepository;
            _configuration = configuration;
        }
        //添加商品
        [HttpPost]
        public ActionResult AddCommodity(ParamCommodity model)
        {
            var commodity = new Commodity
            {
                CommodityName = model.CommodityName,
                CommodityTypeId = model.CommodityTypeId,
                MerchantId = model.MerchantId,
                CommodityMoney = model.CommodityMoney
            };
            _commodityRepository.Insert(commodity);
            return Ok(new
            {
                Code = 1000,
                Data = commodity,
                Msg = "添加成功"
            });
        }
        //根据商品id查找
        [HttpGet("GetId/{id}")]
        public ActionResult GetCommodityId(int id)
        {
            var commoditys = _commodityRepository.GetById(id);
            return Ok(new
            {
                Code = 1000,
                Data = commoditys,
                Msg = "获得成功"
            });
        }
        //根据不同的商品分类获得不同的商品
        [HttpGet("GetType/{id}")]
        public ActionResult GetTypeId(int id)
        {
            var commoditys = _commodityRepository.Table;
            var Commodity = commoditys.Where(x => x.CommodityTypeId == id);
            return Ok(new
            {
                Code = 1000,
                Data = Commodity,
                Msg = "获得成功"
            });
        }
        //获得所有商品
        [HttpGet]
        public ActionResult GetCommodity()
        {
            var commoditys = _commodityRepository.Table;
            return Ok(new
            {
                Code = 1000,
                Data = commoditys,
                Msg = "获得成功"
            });
        }
        //更改商品信息
        [HttpPut("{id}")]
        public ActionResult UpdateCommodity(ParamCommodity model, int id)
        {
            var commodity = _commodityRepository.GetById(id);
            if (commodity != null)
            {
                commodity.CommodityName = model.CommodityName;
                commodity.CommodityTypeId = model.CommodityTypeId;
                commodity.MerchantId = model.MerchantId;
                commodity.CommodityMoney = model.CommodityMoney;
            }
            _commodityRepository.Update(commodity);
            return Ok(new
            {
                Code = 1000,
                Data = commodity,
                Msg = "修改成功"
            });
        }
        //删除商品
        [HttpDelete("{id}")]
        public ActionResult DeletedCommodity(int id)
        {
            var Commodity = _commodityRepository.GetById(id);
            if (Commodity != null)
            {
                Commodity.IsDeleted = true;
                _commodityRepository.Update(Commodity);
                return Ok(new
                {
                    Code = 1000,
                    Data = Commodity,
                    Msg = "删除成功"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = Commodity,
                Msg = "删除失败"
            });
        }

        //根据商品名模糊查询
        [HttpGet("CommodityNames")]
        public ActionResult GetCommodityName(ParamCommodity model)
        {
            var commoditys = _commodityRepository.Table;
            var commodity = commoditys
            .Where(x => x.CommodityName.Contains(model.CommodityName))
            .Where(x => x.IsDeleted == false).ToList();

            if (commodity != null)
            {
                return Ok(new
                {
                    Code = 1000,
                    Data = commodity,
                    Msg = "获得成功"
                });
            }
            else
            {
                return Ok(new
                {
                    Code = 1000,
                    Data = " ",
                    Msg = "获得失败"
                });
            }
        }
    }


}